﻿using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class AdminCP_CuentasAlumnos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SessionValidation();
        ODSGlobal.SelectParameters["QueryString"].DefaultValue = "SELECT * FROM (SELECT Alumnos.Matricula, " +
                                   "Alumnos.Nombre + ' ' + Alumnos.Apellido AS [NombreAlumno], " +
                                   "CONVERT(VARCHAR(25), Alumnos.FechaCreacion, 0) AS FechaDeCreacion, " +
                                   "ROW_NUMBER() OVER (ORDER BY Alumnos.FechaCreacion DESC) AS [RowNumber] " +
                                   "FROM Alumnos WHERE Alumnos.CuentaValidada = 0) AS TEMP " +
                                   "WHERE [RowNumber] BETWEEN @startRowIndex AND @lastRow" + 
                                   "#SELECT COUNT(*) FROM Alumnos WHERE Alumnos.CuentaValidada = 0";
    }

    // Activa las cuentas de los alumnos que estén pendientes, o purgarlas
    protected void ValidarCuenta(Object sender, CommandEventArgs a)
    {
        DatabaseConnection dbCon = new DatabaseConnection();
        String Matricula = a.CommandArgument.ToString();
        String query = "UPDATE Alumnos SET Alumnos.CuentaValidada = 1 WHERE Alumnos.Matricula = @Matricula";

        using (SqlCommand activaCuenta = new SqlCommand(query))
        {
            activaCuenta.Parameters.Add(new SqlParameter("@Matricula", Matricula));
            dbCon.ParameterizedExecuteNonQuery(activaCuenta);
        }
        dbCon.CloseConnection();

        Response.Redirect("~/AdminCP/CuentasAlumnos.aspx");
    }


    // Valida que la sesión de Admin sea valida y/o exista
    protected void SessionValidation()
    {
        String SessionID = (String)Session["SessionID"];
        String UserType = (String)Session["UserType"];

        if (String.IsNullOrEmpty(SessionID) || String.IsNullOrEmpty(UserType))
        {
            Session.RemoveAll();
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            if (!String.Equals(UserType, "Administradores"))
            {
                Session.RemoveAll();
                Response.Redirect("~/Default.aspx");
            }
        }
    }

}